package com.netsdk.lht2.push;

import com.netsdk.demo.util.sql.Trajectory;
import com.netsdk.lht2.util.TrajectoryUtil;


import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class RoadDataStatistic {
    RoadVehicleDataCblPcl roadVehicleDataCblPcl ;
    RoadVehicleDataSxlCjsl roadVehicleDataSxlCjsl ;
    LocalDateTime addTime = LocalDateTime.now();
    LocalDateTime endTime = LocalDateTime.now();
    String utc;

    public RoadDataStatistic(String utc) {
        this.utc = utc;
        roadVehicleDataCblPcl = new RoadVehicleDataCblPcl(utc);
        roadVehicleDataSxlCjsl = new RoadVehicleDataSxlCjsl(utc);
    }

    public void addData(String channel, String nlane, List<Trajectory[]> arr){
        if (roadVehicleDataCblPcl.support(channel)){
            roadVehicleDataCblPcl.addData(channel,nlane,arr);
        }else if(roadVehicleDataSxlCjsl.support(channel)) {
            roadVehicleDataSxlCjsl.addData(channel,nlane,arr);
        }
        endTime = LocalDateTime.now();
    }

//    public String getPushData(){
//         String pushData1 = roadVehicleDataCblPcl.getPushData();
//        String pushData2 = roadVehicleDataSxlCjsl.getPushData();
//        return pushData1+pushData2;
////        return pushData1;
//    }

    public String getPushData(){
        List<Trajectory[]> pushData1 = roadVehicleDataCblPcl.getPushData();
        System.out.println("城北路车辆总数："+ roadVehicleDataCblPcl.countTotal());
        List<Trajectory[]> pushData2 = roadVehicleDataSxlCjsl.getPushData();
        System.out.println("胜辛路车辆总数："+ roadVehicleDataSxlCjsl.countTotal());
        pushData1.addAll(pushData2);
        // 排序
        List<Trajectory> data =new ArrayList<>();
        pushData1.forEach(el-> data.addAll(Arrays.asList(el)));
        pushData2.forEach(el-> data.addAll(Arrays.asList(el)));
        data.sort(Comparator.comparing(Trajectory::getTime));
        StringBuilder sb = new StringBuilder();
        for (Trajectory trajectory : data) {
            String body = TrajectoryUtil.toPushString(trajectory);
            sb.append(body);
        }
        return sb.toString();
    }

    public void  printAddTimeInfo(){
       // System.out.println(String.format("统计区间：%s 开始添加时间：%s,最后添加时间：%s，差值：%s",utc,addTime,endTime, Duration.between(addTime,endTime).toMillis()));;
    }

}
